﻿using Dexter.Logger.Contracts;
using Dexter.Scheduler;
using Dexter.Services;

namespace Dexter.Job.AutoCloseComment {
	/// <summary>
	/// The implementation of <see cref="IFrequentlyJob"/>. This job will disable the comment for the old post.
	/// </summary>
	public class AutoCloseCommentJob : IFrequentlyJob {
		readonly ILogger logger;
		readonly IConfigurationService configurationService;
		readonly IPostService postService;

		/// <summary>
		/// Initializes a new instance of the <see cref="AutoCloseCommentJob"/> class.
		/// </summary>
		/// <param name="logger">The logger.</param>
		/// <param name="configurationService">The configuration service.</param>
		/// <param name="postService">The post service.</param>
		public AutoCloseCommentJob ( ILogger logger , IConfigurationService configurationService , IPostService postService ) {
			this.logger = logger;
			this.postService = postService;
			this.configurationService = configurationService;
		}

		/// <summary>
		/// Gets a value indicating whether [auto manage session].
		/// </summary>
		/// <value><c>true</c> if [auto manage session]; otherwise, <c>false</c>.</value>
		public bool AutoManageSession {
			get { return true; }
		}

		/// <summary>
		/// Gets the name of the group.
		/// </summary>
		/// <value>The name of the group.</value>
		public string GroupName {
			get { return null; }
		}

		/// <summary>
		/// Set to <c>false</c> if the state of Job will be reused from the next startup. Otherwise <c>false</c>.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is volatile; otherwise, <c>false</c>.
		/// </value>
		public bool IsVolatile {
			get { return false; }
		}

		/// <summary>
		/// Gets the unique id.
		/// </summary>
		/// <value>The unique id.</value>
		public string UniqueId {
			get { return "{367a736b-5729-417d-af6b-9a3675c05858}"; }
		}

		/// <summary>
		///This method will be exececuted by the <see cref="IScheduler"/>
		/// </summary>
		/// <remarks>
		///	The execution interval depends by <see cref="ICronJob"/> or <see cref="IFrequentlyJob"/>.
		/// </remarks>
		public void Execute ( ) {
			logger.Debug ( "Executing PublishItemJob started" );

			var configuration = configurationService.Configuration;

			if(configuration.CommentConfiguration.DayBeforeDisableComment == null)
				return;

			//the pagesize is small becouse the sql transaction could lock too many data.
			//the next job execution will disable next 50.

			var data = postService.GetPublishedPostWithCommentsDisabled ( 0 , 50 );

			if(data.TotalCount > 1) {
				foreach ( var post in data.Result ) {
					if (post == null) {
						logger.Fatal ( "Houston we have a problem!" );
						continue;
					}

					post.DisableComment (  );
					postService.Update ( post );
				}
			}

			logger.Debug ( "Execution 'PublishItemJob' completed" );
		}

		/// <summary>
		/// The elapsed minutes from a job execution and the next one.
		/// </summary>
		/// <value>The minutes.</value>
		public int Minutes {
			get { return 60; }
		}
	}
}
